package com.itguigu.auth.activiti;

import org.activiti.engine.RepositoryService;
import org.activiti.engine.RuntimeService;
import org.activiti.engine.TaskService;
import org.activiti.engine.repository.Deployment;
import org.activiti.engine.runtime.ProcessInstance;
import org.activiti.engine.task.Task;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;

import java.util.HashMap;
import java.util.List;

/**
 * 请假流程测试 - 排他网关
 */
@SpringBootTest
public class ProcessTestqingjia003 {

    @Autowired
    private RepositoryService repositoryService;


    /**
     * 1流程定义部署
     */
    @Test
    public void deployProcess() {
        Deployment deploy = repositoryService.createDeployment()
                .addClasspathResource("process/qingjia003.bpmn20.xml")
                .addClasspathResource("process/qingjia003.png")
                .name("请假申请流程003")
                .deploy();
        System.out.println(deploy.getId());
        System.out.println(deploy.getName());
    }

    @Autowired
    private RuntimeService runtimeService;

    /**
     * 2开启流程实例
     */
    @Test
    public void startUpProcessInstance() {

        ProcessInstance processInstance = runtimeService.startProcessInstanceByKey("qingjia003");
        System.out.println("processInstance.getProcessDefinitionId() = " + processInstance.getProcessDefinitionId());
        System.out.println("processInstance.getId() = " + processInstance.getId());
    }

    @Autowired
    private TaskService taskService;

    /**
     * 3. 查询代办任务
     */
    @Test
    public void findTaskList() {
        List<Task> list = taskService.createTaskQuery()
//                .taskAssignee("wangwu")
//                .taskAssignee("gouwa")
                .taskAssignee("xiaoli")
                .list();
        for (Task task : list) {
            System.out.println("task.getProcessDefinitionId() = " + task.getProcessDefinitionId());
            System.out.println("task.getName() = " + task.getName());
            System.out.println("task.getId() = " + task.getId());
            System.out.println("task.getAssignee() = " + task.getAssignee());
        }
    }

    /**
     * 4. 办理任务
     */
    @Test
    public void complateTask() {
        Task task = taskService.createTaskQuery()
//                .taskAssignee("wangwu")
                .taskAssignee("gouwa")
                .singleResult();

        taskService.complete(task.getId());
    }

}
